#################################################
#################################################
#################################################
#################################################
####### Tues 31st December 2011
#################################################


import generateTable
import expandTable
import queryTable
import normalizeTable
import generateSuperGraph
import outputFunctions
import os
import subprocess
import utilityFunctions


#################################################
#################################################

print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "\n"
print "PYMED - Thursday 16 February 2012"

print "\n"
print "\n"


# Options for preference criteria
# (i.e. for choosing which attacks
# hold between inductive arguments)
# are:
# "prefcriterion1"
# "prefcriterion2"
# "prefcriterion3"
#
# The first counts all outcomes,
# the second counts all non-tox outcomes
# and the third prefers args without
# death tox rate > 0.01%, and then
# for the remainder counts all
# outcomes

prefcriterion_A = "prefcriterion1"
prefcriterion_B = "prefcriterion2"
prefcriteria = [prefcriterion_A, prefcriterion_B]
# Choice of metarules for generating meta-arguments
# is given by the metarules parameter (a list of rule names)
# For rule name in the list, the metarule will be used
# to generate meta-arguments.
#
# Choices are:
#
# - nonEuropean (i.e. some evidence is from non European country)
# - SEAsian (i.e. some evidence is from SE Asian country)
# - US (i.e. some evidence is from US)
# - nonStatSig (i.e. some evidence is not statistically significant)
# - induction (i.e. for pro argument, when left treatment uses induction,
#              and for con argument, when right treatment uses induction).
#
# Zero or more choices can appear in the metarules list.

#metarules = ["US"]
#metarules = ["US","induction"]
#metarules = ["induction"]
#metarules = ["nonStatSig"]
#metarules = ["PhaseII"]
#metarules = ["StageII"]
metarules_A = []
metarules_B = []
metarules = [metarules_A, metarules_B]

#outputdirectory = "../Output"
inputdirectory = '../Input/'


filename = 'Lung_ChemoRT_M0.5_Cochrane_Base.csv'
#filename = 'Lung_ChemoRT_M0.5_Cochrane_Base_RTChemo_Relax.csv'
#filename = 'Lung_ChemoRT_M0.5_Cochrane_Ext_RTChemo_Relax.csv'
#filename = 'Lung_ChemoRT_Merged_0.5_RTChemo_Relax.csv'

inputFileName = inputdirectory + filename

OutputName = filename
outputFile = "output.html"
dotOutputName = OutputName + '.dot'

print "Read following spreadsheet"

print inputFileName

evdb = generateTable.getmydb(inputFileName)

evdb = expandTable.addriskratio(evdb)

#for x in evdb:
#    print x

#mykeys = evdb[0].keys()

#for c in mykeys:
#    print c


#print "\n"
#print "\n"
#print "Using following rows from spreadsheet (possibly with offset of 1)"

#print queryTable.getmyidlist(evdb)

#print "\n"
#print "\n"
#print "Treatments appearing in spreadsheet"

treatments = queryTable.getmyoplist(evdb)

#for x in treatments:
#    print x

#print "\n"
#print "\n"

#for x in evdb:
#    print x['myid']

# print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"

# print "\n"
# print "\n"
# print "Start superiority graph construction"
# print "\n"
# print "\n"


# print "\n"
# print "Treatment pairs that appear in spreadsheet"
# print "(i.e. there is at least one row in the"
# print "spreadsheet comparing the treatments)"
# print "\n"

# print "Call queryTable.getmyoppairs"


treatmentpairs = queryTable.getmyoppairs(evdb)

#for p in treatmentpairs:
#    print p

#print "\n"

#print "Call generateSuperGraph.filterpairs"

selectedpairs = generateSuperGraph.filterpairs(treatmentpairs,evdb)

#for p in selectedpairs:
#    print p
    
print "\n"
print "\n"

print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"


superResults_A = generateSuperGraph.makeSuperGraph(selectedpairs,evdb,prefcriterion_A,metarules_A)
superResults_B = generateSuperGraph.makeSuperGraph(selectedpairs,evdb,prefcriterion_B,metarules_B)


taggedSuperResults = outputFunctions.simpleDiff([superResults_A, superResults_B])        

print "Tagged SR:", taggedSuperResults
# print "\n"
# print "\n"

# print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
# print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"


# print "\n"
# print "Super graph arcs"
# print "\n"

#for x in superResults_A, superResults_B:
#    print x

print "\n"
print "This superiority graph was constructed using"
print "the following preference criterion over inductive"
print "arguments (for determining the strict attack"
print "relation."
print "\n"
print prefcriterion_A, prefcriterion_B
print "\n"
print "and the following meta rules used for generating"
print "the meta-arguments"
print "\n"
print metarules_A, metarules_B
print "\n"




###########################################
###########################################
#From here we manage the output
#dir = os.getcwd()
#print "Using dir: ", dir, " as a base."

outputDirectory = "../Output/"

#print "\n"

####These make the top-level files: A normalised evidence table and a picture
#newOutDir = str(dir) + '\\' + dotOutputName.rstrip('.dot') + '_dir'
subOutDir = outputDirectory  + OutputName + '_dir' + '/'

print "Using dir: ", outputDirectory, " as a base."
print "Using dir: ", subOutDir, " as a subDir"

utilityFunctions.ensure_dir(subOutDir)

###We've made a sub-directory, so now fill it with subtables
outputFunctions.generateSubtables(evdb, selectedpairs, [superResults_A, superResults_B], subOutDir, metarules_A, prefcriterion_A)
outputFunctions.generateDotFile(inputFileName, dotOutputName, prefcriteria, metarules, taggedSuperResults, subOutDir)
outputFunctions.generateNormalisedTables(evdb, outputFile, dotOutputName)

dotFullPath = subOutDir + dotOutputName
#print "Calling dot on: ", dotFullPath
subprocess.call(['dot', '-Tsvg', '-O', dotFullPath], shell = True)
imgOutputName = subOutDir  + dotOutputName + '.svg'
#print "SVG Outputname ", imgOutputName

os.startfile(os.path.abspath(imgOutputName))
#os.startfile(os.path.abspath(outputFile))


#print "\n\n\n"

#normalTable = normalizeTable.makeTable(evdb)

#for x in normalTable:
#    print x.get('Stage','unknown')

#for x in normalTable:
#    i = x.get('myid','error')
#    print i
#    r = getFullRow(i,evdb)
#    print r.get('RecruitmentArea','unknown')
    
 


# In generateSuperGraph module, the subtable for each
# treatment pair is identified. Then the normalized version
# is produced. So the numbering for each item of normalized table
# is just for that subtable. 

#################################################
#################################################
#################################################
#################################################

